package com.iamteer.d_fileupload;

import java.io.File;
import java.io.InputStream;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {
	public String list() throws Exception {
		String path = ServletActionContext.getServletContext().getRealPath("/upload");
		File file = new File(path);
		String[] fileNames = file.list();
		
		ActionContext ac = ActionContext.getContext();
		Map<String, Object> request = (Map<String, Object>) ac.get("request");
		request.put("fileNames", fileNames);
		return "list";
	}
	
//******************************************
	// 1. 文件名
	private String fileName;
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileName() {
		return fileName;
	}
	
	// 2. 下载提交的业务方法
	public String down() throws Exception {
		return "download";
	}
	
	// 3. 返回文件流
	public InputStream getAttrInputStream() {
		return ServletActionContext.getServletContext().getResourceAsStream("/upload/" + fileName);
	}
}
